﻿using System.Xml.Linq;
using BattleFury.Lib.Extensions;
using BattleFury.Lib.Input;
using BattleFury.Lib.Sprites;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace BattleFury.Lib.Controls
{
	public abstract class Control
		: Sprite, IControl
	{
		public string Name { get; set; }

		public Skin Skin { get; set; }
		public SpriteFont Font { get; set; }

		public override Texture2D Texture { get { return Skin.Texture; } }
		public virtual InputManager Input { get { return RenderEngine.Get().Input; } }

		public SpriteBatch SpriteBatch { get { return RenderEngine.Get().SpriteBatch; } }

		protected Control()
		{
			IsVisible = true;
			IsEnabled = true;
			Tint = Color.White;
		}

		public virtual void Serialize()
		{

		}

		public virtual void Deserialize(XElement element, object callingObject)
		{
			Name = element.GetString("Name");
			Opacity = element.GetFloat("Opacity", 1f);
			IsEnabled = element.GetBoolean("IsEnabled", true);
			IsVisible = element.GetBoolean("IsVisible", true);

			var x = element.GetInt("X", 0);
			var y = element.GetInt("Y", 0);
			var width = element.GetInt("Width", 32);
			var height = element.GetInt("Height", 32);

			Position = new Vector2(x, y);
			Size = new Vector2(width, height);
		}
	}
}
